<?php

namespace app\admin\service;

use app\api\service\UserService;
use app\common\model\User as UserModel;
use app\common\model\Withdraw as WithdrawModel;
use app\common\service\BaseAdminService;
use app\exception\ApiException;
use think\Exception;
use think\facade\Db;

class WithdrawService extends BaseAdminService
{
    /**
     * 提现审核通过
     * @param $withdraw
     * @return int
     * @throws Exception
     */
    public static function pass($withdraw){
        if ($withdraw->state != 0) {
            return self::error('订单状态不正确');
        }
        $channel  = 'PayWilk';
        try {
            //开启事务
            Db::startTrans();
            $withdraw->state = 1;
            $withdraw->completed_time = date('Y-m-d H:i:s');
            $obj = "\\payment\\PayWilk";
            $pay = new $obj($withdraw);
            $result = $pay->withdraw();
            if (!empty($result['code'])) {
                if (isset($result['exists'])&&$result['exists']){
                    $withdraw->payed_from = $channel;
                    $withdraw->save();
                    DB::commit();
                    return self::success('订单'.$withdraw->pay_sn.'重复提交，让通道那边重新回调一下');
                }else{
                    throw new Exception('支付通道异常：'.$result['message'], 1);
                }
            }
            $withdraw->pay_sn = $result['platOrderId'] ?? '';
            DB::commit();
            return self::success('订单'.$withdraw->pay_sn.'提交成功');
        } catch (\Exception $e) {
            DB::rollBack();
            return self::error($e->getMessage());
        }
    }

    /**
     * 提现失败回调成功后台操作
     * @param $withdraw_id
     */
    public static function withdrawFail($withdraw_id){
        $order = WithdrawModel::find($withdraw_id);
        if ($order->state != 2){
            return self::error('状态错误，只能操作已通过的提现申请');
        }
        Db::startTrans();
        try {
            $order->state = -2;
            $order->completed_time = date('Y-m-d H:i:s');
            $order->remark = '';
            $order->save();

            //账户余额加款
            $user = UserModel::find($order->user_id);
            UserService::amountAction($user, $order->amount, 'withdraw.pay_fail');

            //之前没有提现成功的话清空银行卡信息
            $count = WithdrawModel::where('user_id', $user->id)->where('state', 1)->count();
            if (!$count) {
                $user->name = '';
                $user->bank_number = '';
                $user->bank_ifsc = '';
                $user->save();
            }

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            return self::error($e->getMessage());
        }
    }


    //提现后台直接完成
    public static function finish($withdraw_id){
        $withdraw = WithdrawModel::find($withdraw_id);
        $withdraw->state = 2;
        $withdraw->remark = '后台直接完成';
        // $withdraw->submitted_at = date('Y-m-d H:i:s');
        // $withdraw->payed_at = date('Y-m-d H:i:s');
        $withdraw->completed_time = date('Y-m-d H:i:s');
        $withdraw->save();
        return true;
    }
}